
<template>
  <section class="wh_container">
    <div class="wh_content_all">
      <div class="wh_top_changge">
        <!-- <li @click="PreMonth(myDate,false)">
          <div class="wh_jiantou1"></div>
        </li> -->
        <li class="wh_content_li">{{dateTop}}</li>
        <!-- <li @click="NextMonth(myDate,false)">
          <div class="wh_jiantou2"></div>
        </li> -->
      </div>
      <div class="wh_content">
        <div class="wh_content_item" v-for="tag in textTop" :key="tag">
          <div class="wh_top_tag">{{tag}}</div>
        </div>
      </div>
      <div class="wh_content">
        <div class="wh_content_item" v-for="(item) in list" :key="item.date" v-bind:class="{wh_other_dayhide:item.otherMonth!=='nowMonth'}">
          <input v-if="item.otherMonth === 'nowMonth'" class="selected" v-model="model" type="checkbox"  :value="item.date"/>
          <div
            class="wh_item_date"
            v-bind:class="[
              { wh_isMark: item.isMark},
              {wh_other_dayhide:item.otherMonth!=='nowMonth'},
              {wh_want_dayhide:item.dayHide},
              {wh_isToday:/*item.isToday*/ false},
              {wh_chose_day: value.includes(item.date)},
              setClass(item)
            ]"
            @click="waitset(item)"
          >{{item.id}}</div>
        </div>
      </div>
    </div>
  </section>
</template>
<script>
import timeUtil from "./calendar"
export default {
  data() {
    return {
      selData: [],
      myDate: [],
      list: [],
      historyChose: [],
      dateTop: "",
      arrayWaitset: []
    }
  },
  props: {
    dateVal: {
      type: String,
      default() {
        return ''
      }
    },
    value: {
      type: Array,
      default() {
        return []
      }
    },
    markDate: {
      type: Array,
      default: () => []
    },
    markDateMore: {
      type: Array,
      default: () => []
    },
    textTop: {
      type: Array,
      default: () => ["一", "二", "三", "四", "五", "六", "日"]
    },
    sundayStart: {
      type: Boolean,
      default: () => false
    },
    agoDayHide: {
      type: String,
      default: `0`
    },
    futureDayHide: {
      type: String,
      default: `2554387200`
    }
  },
  computed: {
    model: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  created() {
    this.intStart()
    this.myDate = new Date(this.dateVal)
  },
  updated() {

  },
  methods: {
    selectVal(v) {
      this.$emit('input', v)
    },
    intStart() {
      timeUtil.sundayStart = this.sundayStart
    },
    setClass(data) {
      // this.$nextTick(()=>{
      const obj = {}
      obj[data.markClassName] = !!data.markClassName
      return obj
      // });
    },
    forMatArgs: function() {
      let markDate = this.markDate
      let markDateMore = this.markDateMore
      markDate = markDate.map(k => {
        return timeUtil.dateFormat(k)
      })
      markDateMore = markDateMore.map(k => {
        k.date = timeUtil.dateFormat(k.date)
        return k
      })
      return [markDate, markDateMore]
    },
    // getList: function(date, chooseDay, isChosedDay = true) {
    getList: function(date, chooseDay) {
      const [markDate, markDateMore] = this.forMatArgs()
      this.dateTop = `${date.getMonth() + 1}月`
      const arr = timeUtil.getMonthList(this.myDate)
      for (let i = 0; i < arr.length; i++) {
        let markClassName = ""
        const k = arr[i]
        k.chooseDay = false
        const nowTime = k.date
        const t = new Date(nowTime).getTime() / 1000
        // 看每一天的class
        for (const c of markDateMore) {
          if (c.date === nowTime) {
            markClassName = c.className || ""
          }
        }
        // 标记选中某些天 设置class
        k.markClassName = markClassName
        k.isMark = markDate.indexOf(nowTime) > -1
        // 无法选中某天
        k.dayHide = t < this.agoDayHide || t > this.futureDayHide
        if (k.isToday) {
          this.$emit("isToday", nowTime)
        }
        const flag = !k.dayHide && k.otherMonth === "nowMonth"
        if (chooseDay && chooseDay === nowTime && flag) {
          this.$emit("choseDay", nowTime)
          this.historyChose.push(nowTime)
          k.chooseDay = true
        } else if (
          this.historyChose[this.historyChose.length - 1] === nowTime &&
          !chooseDay &&
          flag
        ) {
          k.chooseDay = true
        }
      }
      this.list = arr
    },
    waitset: function() {
      this.$emit("updata", this.arrayWaitset)
    }

  },
  mounted() {
    this.getList(this.myDate)
  },
  watch: {
    dateVal: {
      handler(val) {
        this.myDate = new Date(val)
        this.getList(this.myDate)
      },
      deep: true
    },
    markDateMore: {
      handler() {
        // this.myDate = new Date(val);
        this.getList(this.myDate)
      },
      deep: true
    },
    agoDayHide: {
      handler() {
        this.getList(this.myDate)
      },
      deep: true
    },
    futureDayHide: {
      handler() {
        this.getList(this.myDate)
      },
      deep: true
    },
    sundayStart: {
      handler() {
        this.intStart()
        this.getList(this.myDate)
      },
      deep: true
    }
  }
}
</script>

<style scoped lang="scss">
@media screen and (min-width: 460px) {
  .wh_item_date:hover {
    background-color: rgb(244, 244, 244);
    cursor: pointer;
    border-radius: 100%;
  }
}

.wh_container {
  max-width: 410px;
  margin: auto;

  margin:0 16px 16px 0;

}
:nth-of-type(4n+4).wh_container{
  margin-right: 0px;
}
:nth-of-type(n+8).wh_container{
  margin-bottom: 0;
}

li {
  list-style-type: none;
}
.wh_top_changge {
  display: flex;
}

.wh_top_changge li {
  cursor: pointer;
  display: flex;
  color: #fff;
  font-size: 18px;
  flex: 1;
  text-indent: 16px;
  align-items: center;
  height: 47px;
}

.wh_top_changge .wh_content_li {
  cursor: auto;
  flex: 2.5;
  color:#2A2C2F;
  font-weight: bold;
  font-size: 16px;
}
.wh_content_all {
  overflow: hidden;
  padding-bottom: 8px;
  border: 1px solid #C4CBE0;
  background-color: #fff;
  width: 370px;
  height: 330px;

}

.wh_content {
  display: flex;
  flex-wrap: wrap;
  padding: 0 3% 0 3%;
  width: 100%;
  font-size: 14px;
}
.wh_content_all::first-of-type.wh_content{
  border-bottom: 1px solid #DFE5F4;
}

.wh_content:first-child .wh_content_item_tag,
.wh_content:first-child .wh_content_item {
  color: #ddd;
  font-size: 16px;
  position:relative;
  border:1px solid rgba(223,229,244,1);
}
.wh_content_item .selected{
  width: 40px;
  height: 40px;
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.wh_content_item,
wh_content_item_tag {
  font-size: 15px;
  width: 13.4%;
  text-align: center;
  color: #fff;
  position: relative;
}
.wh_top_tag {
  width: 40px;
  height: 40px;
  line-height: 40px;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  color:rgba(99,101,105,1);
}

.wh_item_date {
  width: 40px;
  height: 40px;
  line-height: 40px;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  color:#4B79FE;
}

.wh_jiantou1 {
  width: 12px;
  height: 12px;
  border-top: 2px solid #ffffff;
  border-left: 2px solid #ffffff;
  transform: rotate(-45deg);
}

.wh_jiantou1:active,
.wh_jiantou2:active {
  border-color: #ddd;
}

.wh_jiantou2 {
  width: 12px;
  height: 12px;
  border-top: 2px solid #ffffff;
  border-right: 2px solid #ffffff;
  transform: rotate(45deg);
}
.wh_content_item > .wh_isMark {
  margin: auto;
  border-radius: 100px;
  background: blue;
  color:#fff;
  z-index: 2;
}
.wh_content_item .wh_other_dayhide {
  display:none;
}
.wh_content_item .wh_want_dayhide {
  color: #bfbfbf;
}
.wh_content_item .wh_isToday {
  background: yellow;
  border-radius: 100px;
}
.wh_content_item .wh_chose_day {
  background: #DAE4FF;
  border-radius: 100px;
}
</style>
